package 并发编程11.ThreadLocal的使用案例;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author idea
 * @data 2019/1/26
 */
public class ThreadTest1 {

    private static ThreadLocal<Integer> local=new ThreadLocal<Integer>(){

        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public int getNextValue(){
        local.set(local.get()+1);
        return local.get();
    }

    public static void main(String[] args) {
        ThreadTest1 t1=new ThreadTest1();
        for(int i=0;i<40;i++){
            ThreadClient tc1=new ThreadClient(t1);
            tc1.start();
            Collections.synchronizedList(new ArrayList<>());
        }

    }

}

class ThreadClient extends Thread{

    private ThreadTest1 th;

    public ThreadClient(ThreadTest1 t){
        this.th=t;
    }

    @Override
    public void run(){
        for(int i=0;i<3;i++){
            System.out.println("thread:"+Thread.currentThread().getName()+":---"+th.getNextValue());
        }
    }

}
